package io.github.loserYa.log;


import org.springframework.util.StringUtils;

/**
 * sql 构建者
 *
 * @author loser
 * @date 2024/4/25
 */
public class SqlBuild {

    private String tableName;

    private String columns;

    private String values;

    private String updates;

    private String condition;

    public static SqlBuild buildInsert(String tableName, String columns, String values) {
        SqlBuild sqlBuild = new SqlBuild();
        sqlBuild.setTableName(tableName);
        sqlBuild.setColumns(columns);
        sqlBuild.setValues(values);
        return sqlBuild;
    }

    public static SqlBuild buildUpdate(String tableName, String updates, String condition) {
        SqlBuild sqlBuild = new SqlBuild();
        sqlBuild.setTableName(tableName);
        sqlBuild.setUpdates(updates);
        sqlBuild.setCondition(condition);
        return sqlBuild;
    }

    public String buildInsert() {
        if (StringUtils.isEmpty(tableName) || StringUtils.isEmpty(columns) || StringUtils.isEmpty(values)) {
            return "";
        }
        return "INSERT INTO " + tableName + " (" + columns + ") VALUES " + values;
    }

    public String buildUpdate() {
        if (StringUtils.isEmpty(tableName) || StringUtils.isEmpty(updates) || StringUtils.isEmpty(condition)) {
            return "";
        }
        return "UPDATE " + tableName + " SET " + updates + " WHERE " + condition;
    }

    public String uniqueKey() {
        return tableName + columns;
    }

    public boolean notNull() {
        return !StringUtils.isEmpty(tableName) && !StringUtils.isEmpty(columns) && !StringUtils.isEmpty(values);
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getColumns() {
        return columns;
    }

    public void setColumns(String columns) {
        this.columns = columns;
    }

    public String getValues() {
        return values;
    }

    public void setValues(String values) {
        this.values = values;
    }

    public String getUpdates() {
        return updates;
    }

    public void setUpdates(String updates) {
        this.updates = updates;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }
}
